//
//  WQCResgistViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/27.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCResgistViewController.h"
#import "configuration.h"
#import "RSA.h"
#import "UIButton+CountDown.h"
#import "WYCArgeeViewController.h"
@interface WQCResgistViewController ()<UITextFieldDelegate>
/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;
@property(nonatomic,strong)UIButton * regsterButton;
@property(nonatomic,strong)UIButton  * loginButton;
@end

@implementation WQCResgistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor] ;
    [self setupNav];
    [self creatUI];
    
}
-(void)creatUI
{
   
    UIView * phoneView = [[UIView alloc] init];
    phoneView.frame = CGRectMake(40 , NavHeight+ 245 * px, ScreenWidth-80, 44);
    phoneView.backgroundColor = WYCRGBColor(249,249, 249);
    phoneView.layer.cornerRadius = 25;
    [self.view addSubview:phoneView];
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(21, 11, 21 , 21);
    iphonelabel.image = [UIImage imageNamed:@"icon_phone"];
    [phoneView addSubview:iphonelabel];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, phoneView.width-60, 20);
    userTextField.backgroundColor = [UIColor clearColor];
    userTextField.placeholder = @"请输入手机号";
    userTextField.tintColor = wycMain;
    userTextField.delegate = self;
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    userTextField.clearButtonMode=UITextFieldViewModeAlways;
    [phoneView addSubview:userTextField];
        self.UserTextField = userTextField;
    

    //密码
    UIView * userView = [[UIView alloc] init];
    userView.frame = CGRectMake(40 , phoneView.y+69, ScreenWidth-80,44);
    userView.backgroundColor = WYCRGBColor(249,249, 249);
    userView.layer.cornerRadius = 25;
    [self.view addSubview:userView];
    
    //密码
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(21, 11, 21, 21);
    passlabel.image = [UIImage imageNamed:@"icon_password"];
    [userView addSubview:passlabel];
    
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, (userView.width - iphonelabel.width + 10 * px) / 2, 20);
    passTextField.backgroundColor = [UIColor clearColor];
    passTextField.placeholder = @"请输入密码";
    passTextField.tintColor = wycMain;
    passTextField.delegate=self;

    passTextField.secureTextEntry = YES;
    [userView addSubview:passTextField];
       self.passtexfield = passTextField;

    //密码
    UIView * frinedView = [[UIView alloc] init];
    frinedView.frame = CGRectMake(40 , userView.y+69, ScreenWidth-80,44);
    frinedView.backgroundColor = WYCRGBColor(249,249, 249);
    frinedView.layer.cornerRadius = 25;
    [self.view addSubview:frinedView];
    
    //密码
    UIImageView * friendlabel = [[UIImageView alloc] init];
    friendlabel.frame = CGRectMake(21, 11, 21, 21);
    friendlabel.image = [UIImage imageNamed:@"friend"];
    [frinedView addSubview:friendlabel];
    
    
    UITextField * friendTextField = [[UITextField alloc] init];
    friendTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, (frinedView.width - iphonelabel.width + 10 * px) , 20);
    friendTextField.backgroundColor = [UIColor clearColor];
    friendTextField.placeholder = @"请输入邀请码";
    friendTextField.tintColor = wycMain;
    friendTextField.delegate=self;
    [frinedView addSubview:friendTextField];
    self.Rgscodefield =friendTextField ;

        //验证码
        UIView * codeView = [[UIView alloc] init];
        codeView.frame = CGRectMake(40 , frinedView.y+69, ScreenWidth-80,44);
        codeView.backgroundColor = WYCRGBColor(249,249, 249);
        codeView.layer.cornerRadius = 25;
    
        [self.view addSubview:codeView];
    
        UIImageView * codelabel = [[UIImageView alloc] init];
        codelabel.frame = CGRectMake(21, 11, 21, 21);
        codelabel.image = [UIImage imageNamed:@"code"];
        [codeView addSubview:codelabel];
    
    
        UITextField * codeTextField = [[UITextField alloc] init];
        codeTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, (codeView.width - iphonelabel.width + 10 * px) / 2, 20);
        codeTextField.backgroundColor = [UIColor clearColor];
        codeTextField.clearButtonMode=UITextFieldViewModeAlways;
        codeTextField.placeholder = @"验证码";
        codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        codeTextField.tintColor = wycMain;
        codeTextField.delegate=self;
        [codeView addSubview:codeTextField];
        _codeTexField = codeTextField;
    ;
        //获取验证码
        UIButton * forgetButton = [[UIButton alloc] init];
        forgetButton.frame = CGRectMake(codeView.width-130, 8, 120, codeView.height-16);
        forgetButton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
        forgetButton.backgroundColor = wycMain;
        forgetButton.layer.cornerRadius = 15;
        [forgetButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [forgetButton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [codeView addSubview:forgetButton];
    
    
    //登录按钮
   _loginButton = [[UIButton alloc] init];
    _loginButton.frame = CGRectMake(codeView.x, CGRectGetMaxY(codeView.frame) + 70, codeView.width, 150 * px);
    _loginButton.backgroundColor = wycMain;

    _loginButton.layer.cornerRadius = 25;
    [_loginButton setTitle:@"注册" forState:UIControlStateNormal];
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
    UILabel  *regstLb = [MyUIClass makeUILabelWithFrame:CGRectMake(_loginButton.x,  kScreenH-60, 120, 40*px) andBackColor:[UIColor clearColor] andText:@"点击注册表示同意" andTextColor:[UIColor lightGrayColor] andFont:[UIFont systemFontOfSize:14] andAlignment:NSTextAlignmentRight];
    [self.view addSubview:regstLb];
    
    //注册按钮
    _regsterButton = [[UIButton alloc] init];
    _regsterButton.frame = CGRectMake(regstLb.frame.origin.x+regstLb.frame.size.width, kScreenH-60,150, 40 * px);
    [_regsterButton setTitle:@"《任性还用户注册协议》" forState:UIControlStateNormal];
    _regsterButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    _regsterButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_regsterButton setTitleColor:wycMain forState:UIControlStateNormal];
    [_regsterButton addTarget:self action:@selector(rigesterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_regsterButton];

}
//注册协议
-(void)rigesterClick
{
    WYCArgeeViewController  *agree = [[WYCArgeeViewController alloc]init];
    [self presentViewController:agree animated:YES completion:nil];
}

- (void)setupNav
{
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 40 * px, 300 * px, 64);
    NavRigsterLabel.textColor = [UIColor blackColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"注册";
    [registerView addSubview:NavRigsterLabel];
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_close"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(40 * px, NavRigsterLabel.height / 3, 40 , 40);
    [registerView addSubview:backbutton];
}
-(void)backClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)loginButtonClick
{
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    // 手机号判断是否为空
    if(self.UserTextField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
   
    //验证码判断是否为空
    if(self.codeTexField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        return;
    }
    if(self.passtexfield.text.length ==0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    if(self.passtexfield.text.length <6)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能少于6位"];
        return;
    }
    if (self.Rgscodefield.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"邀请码不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
    NSString *pas = [RSA encryptString:passStr publicKey:RSA_P_KEY];
    
    params[@"mobile"] = self.UserTextField.text;
    params[@"password"] = pas;
    params[@"authcode"] = self.codeTexField.text;
    params[@"regCode"] = self.Rgscodefield.text;
    NSLog(@"请求%@",params);
    
    [HttpTool POST:Register params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"返回%@",responseObject);
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSLog(@"请求%@返回%@",params,responseObject);
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [self dismissViewControllerAnimated:YES completion:nil];

          
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.UserTextField) {
        if (textField.text.length > 10) {
            return NO;
        }
    }
    return YES;
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
    if (self.UserTextField.text.length == 0) {
        
        NSString * showStr = @"手机号不能为空";
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
        return;
    }else{
        

        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"mobile"] = self.UserTextField.text;
        params[@"type"] = @"reg";
      //  NSLog(@"%@",params);
        [HttpTool POST:Rcode params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0)
            {
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:wycMain countColor: [UIColor lightGrayColor]];
            }else{
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
}


@end
